﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;

namespace Hotfix
{
    [MessageHandler(AppType.Map)]
    public class JoinRoomHandle : AMRpcHandler<C2M_Join_Room, M2C_Join_Room>
    {
        protected override void Run(Session session, C2M_Join_Room message, Action<M2C_Join_Room> reply)
        {
            M2C_Join_Room response = new M2C_Join_Room();
            try
            {
                Room room = Game.Scene.GetComponent<RoomManagerComponent>().GetRoom(message.RoomName);
                if (room == null)
                {
                    response.Error = ErrorCode.ERR_JoinRoomError;
                    reply(response);
                    return;
                }
                if (!room.GetComponent<RoomSessionComponent>().SessionIds.Contains(session.Id))
                    room.GetComponent<RoomSessionComponent>().SessionIds.Add(session.Id);
                else
                {
                    Log.Debug("已在房间中");
                }
                Log.Debug(room.RoomName+"Number"+room.GetComponent<RoomSessionComponent>().SessionIds.Count);
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
    }
}
